package cn.px.service.fill.impl;

import com.github.pagehelper.PageHelper;
import cn.px.common.bean.AbstractPageResultBean;
import cn.px.common.model.UserInfo;
import cn.px.common.util.IdGeneratorUtils;
import cn.px.common.util.ListUtils;
import cn.px.common.util.SecurityUtils;
import cn.px.dao.bean.MyPage;
import cn.px.dao.bean.MyVOPage;
import cn.px.dao.dto.fill.FillGatherInfoDTO;
import cn.px.dao.entity.fill.FillGatherInfo;
import cn.px.dao.mapper.fill.FillGatherInfoMapper;
import cn.px.dao.vo.query.fill.FillGatherInfoVO;
import cn.px.service.base.impl.BaseServiceImpl;
import cn.px.service.fill.FillGatherInfoService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description 填报项目信息关联 service 实现类
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2020,北京乾元坤和科技有限公司 All rights reserved.
 */
@AllArgsConstructor
@Service("fillGatherInfoService")
public class FillGatherInfoServiceImpl extends BaseServiceImpl<FillGatherInfoMapper, FillGatherInfo, FillGatherInfoDTO> implements FillGatherInfoService {

    @Override
    public int batchDelete(List<String> ids) {
        Weekend<FillGatherInfo> weekend = new Weekend<>(FillGatherInfo.class);
        WeekendCriteria<FillGatherInfo, Object> weekendCriteria = weekend.weekendCriteria();
        weekendCriteria.andIn(FillGatherInfo::getId, ids);
        mapper.deleteByExample(weekend);
        return 1;
    }

    @Override
    public void saveByGatherAndInfos(FillGatherInfoDTO model) {
        List<String> infoIds = model.getInfoIds();
        UserInfo userInfo = SecurityUtils.getUserInfo();
        LocalDateTime now = LocalDateTime.now();
        List<FillGatherInfo> saveList = ListUtils.newArrayList(infoIds.size());
        infoIds.forEach(infoId -> {
            FillGatherInfo info = new FillGatherInfo();
            info.setCreatedUserId(userInfo.getUserId());
            info.setCreatedUserName(userInfo.getRealName());
            info.setCreatedTime(now);
            info.setUpdateUserId(userInfo.getUserId());
            info.setUpdateUserName(userInfo.getRealName());
            info.setUpdateTime(now);
            info.setInfoId(infoId);
            info.setGatherId(model.getGatherId());
            info.setId(IdGeneratorUtils.getObjectId());
            saveList.add(info);
        });
        mapper.insertListforComplex(saveList);
    }

    @Override
    public MyVOPage<FillGatherInfoVO> selectPageList(FillGatherInfoDTO record, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new MyVOPage<>(mapper.selectList(record));
    }

    @Override
    public AbstractPageResultBean<FillGatherInfo> selectPageInfo(FillGatherInfoDTO record, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new MyPage<>(this.getModelListByDTO(record));
    }
}
